package com.cn.cash.baselib.util;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.support.v4.app.FragmentActivity;
import android.widget.ImageView;

import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.signature.ObjectKey;
import com.cn.cash.baselib.GlideApp;
import com.cn.cash.baselib.GlideOptions;
import com.cn.cash.baselib.GlideRequest;
import com.cn.cash.baselib.GlideRequests;
import com.cn.cash.baselib.R;

import java.io.File;

/**
 * 图片加载器
 */
public class ImageLoader {
    private Activity activity;
    private FragmentActivity fragmentActivity;
    private Fragment fragment;
    private android.support.v4.app.Fragment fragmentV4;
    private Context context;

    public ImageLoader(Activity activity) {
        this.activity = activity;
    }

    public ImageLoader(FragmentActivity fragmentActivity) {
        this.fragmentActivity = fragmentActivity;
    }

    public ImageLoader(Fragment fragment) {
        this.fragment = fragment;
    }

    public ImageLoader(android.support.v4.app.Fragment fragmentV4) {
        this.fragmentV4 = fragmentV4;
    }

    public ImageLoader(Context context) {
        this.context = context;
    }

    public void loadCircleAvatar(ImageView imgView, String url) {
        loadCircleAvatar(imgView,url,R.mipmap.ic_avatar_default);
    }
    public void loadCircleAvatar(ImageView imgView, String url,int placeholder) {
        if (imgView == null) {
            return;
        }
        imgView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        GlideRequests glideRequests = getGlideRequests();
        glideRequests.load(url).transform(new CircleCrop()).placeholder(placeholder)
                .into(imgView);
    }

    public void load(ImageView imgView, String url) {
        load(imgView, url, 0, null, null, null);
    }

    public void load(ImageView imgView, String url, int placeholderResId) {
        load(imgView, url, placeholderResId, null, null, null);
    }
    public void load(ImageView imgView, String url, ImageView.ScaleType scaleType) {
        load(imgView, url, 0, scaleType, null, null);
    }

    public void load(ImageView imgView, String url, RequestListener listener) {
        load(imgView, url, 0, null, null, listener);
    }

    public void load(ImageView imgView, String url, ImageView.ScaleType scaleType, RequestListener listener) {
        load(imgView, url, 0, scaleType, null, listener);
    }

    public void load(ImageView imgView, String url, int placeholderResId, ImageView.ScaleType scaleType) {
        load(imgView, url, placeholderResId, scaleType, null, null);
    }

    public void load(ImageView imgView, String url, int placeholderResId, ImageView.ScaleType scaleType, String signature, RequestListener listener) {
        if (imgView == null) {
            return;
        }
        if (scaleType == null) {
            imgView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        } else {
            imgView.setScaleType(scaleType);
        }
        GlideRequests glideRequests = getGlideRequests();
        if (glideRequests != null) {
            GlideRequest glideRequest = glideRequests.load(url).transition(DrawableTransitionOptions.withCrossFade());
            if (placeholderResId != 0) {
                glideRequest.placeholder(placeholderResId);
            } else {
                glideRequest.placeholder(R.mipmap.ic_placeholder);
            }
            if (signature != null) {
                glideRequest.signature(new ObjectKey(signature));
            }
            if (listener != null) {
                glideRequest.listener(listener);
            }
            glideRequest.into(imgView);
        }
    }

    public void load(ImageView imgView, String url, int placeholderResId, ImageView.ScaleType scaleType, RequestListener listener) {
        if (imgView == null) {
            return;
        }
        if (scaleType == null) {
            imgView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        } else {
            imgView.setScaleType(scaleType);
        }
        GlideRequests glideRequests = getGlideRequests();
        if (glideRequests != null) {
            GlideRequest glideRequest = glideRequests.load(url).transition(DrawableTransitionOptions.withCrossFade());
            if (placeholderResId != 0) {
                glideRequest.placeholder(placeholderResId);
            } else {
                glideRequest.placeholder(R.mipmap.ic_placeholder);
            }
            if (listener != null) {
                glideRequest.listener(listener);
            }
            glideRequest.into(imgView);
        }
    }

    public void load(ImageView imgView, int loadResId, int placeholderResId, ImageView.ScaleType scaleType, String signature) {
        if (imgView == null) {
            return;
        }
        if (scaleType == null) {
            imgView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        } else {
            imgView.setScaleType(scaleType);
        }
        GlideRequests glideRequests = getGlideRequests();
        if (glideRequests != null) {
            GlideRequest glideRequest = glideRequests.load(loadResId).transition(DrawableTransitionOptions.withCrossFade());
            if (placeholderResId != 0) {
                glideRequest.placeholder(placeholderResId);
            } else {
                glideRequest.placeholder(R.mipmap.ic_placeholder);
            }
            if (signature != null) {
                glideRequest.signature(new ObjectKey(signature));
            }
            glideRequest.into(imgView);
        }
    }

    public void load(ImageView imgView, Bitmap bitmap, int placeholderResId, ImageView.ScaleType scaleType, String signature) {
        if (imgView == null) {
            return;
        }
        if (scaleType == null) {
            imgView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        } else {
            imgView.setScaleType(scaleType);
        }
        GlideRequests glideRequests = getGlideRequests();
        if (glideRequests != null) {
            GlideRequest glideRequest = glideRequests.load(bitmap).transition(DrawableTransitionOptions.withCrossFade());
            if (placeholderResId != 0) {
                glideRequest.placeholder(placeholderResId);
            } else {
                glideRequest.placeholder(R.mipmap.ic_placeholder);
            }
            if (signature != null) {
                glideRequest.signature(new ObjectKey(signature));
            }
            glideRequest.into(imgView);
        }
    }

    public void load(ImageView imgView, Uri uri, int placeholderResId, ImageView.ScaleType scaleType, String signature) {
        if (imgView == null) {
            return;
        }
        if (scaleType == null) {
            imgView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        } else {
            imgView.setScaleType(scaleType);
        }
        GlideRequests glideRequests = getGlideRequests();
        if (glideRequests != null) {
            GlideRequest glideRequest = glideRequests.load(uri).transition(DrawableTransitionOptions.withCrossFade());
            if (placeholderResId != 0) {
                glideRequest.placeholder(placeholderResId);
            } else {
                glideRequest.placeholder(R.mipmap.ic_placeholder);
            }
            if (signature != null) {
                glideRequest.signature(new ObjectKey(signature));
            }
            glideRequest.into(imgView);
        }
    }

    private GlideRequests getGlideRequests() {
        if (activity != null) {
            return GlideApp.with(activity);
        } else if (fragmentActivity != null) {
            return GlideApp.with(fragmentActivity);
        } else if (fragment != null) {
            return GlideApp.with(fragment);
        } else if (fragmentV4 != null) {
            return GlideApp.with(fragmentV4);
        } else if (context != null) {
            return GlideApp.with(context);
        }
        return null;
    }

}
